<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';

$data = sanitize($_GET);
if ($data['status'] && isset($data['username'])) {
	$request = new makeRequest($backend.'api.php?method=user&username='.$data['username'], 'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	/* Check for errors */
	if($http_code != 200){
		header('location: profile.php?status=false');
	}
}else{
	// If there is no get input for username the webpage will assume that you want to see your own profile
	if($_GET['status']!= "false"){
		header("location: profile.php?status=true&username=$_SESSION[username]");
	}
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php'
?>
<div id="content"><?php
// Content shown on page
if($data['status']!="false"){ // if there is some info to display
	echo
	'<table><tr><td>Username:</td><td>'.$obj[0]->{'username'}.'</td></tr>
	<tr><td>Mail:</td><td>'.$obj[0]->{'mail'}.'</td></tr>
	<tr><td>Firstname:</td><td>'.$obj[0]->{'firstname'}.'</td></tr>
	<tr><td>Lastname:</td><td>'.$obj[0]->{'lastname'}.'</td></tr>
	<tr><td>Age:</td><td>'.$obj[0]->{'age'}.'</td></tr>
	<tr><td>Location:</td><td>'.$obj[0]->{'location'}.'</td></tr>
	<tr><td>Description:</td><td>'.$obj[0]->{'description'}.'</td></tr></table>';

	if($obj[0]->{'username'} == $_SESSION['username']){
		echo '<a href="editprofile.php" target="_self">Edit profile</a>
			<a href=deleteaccount.php>Delete account</a>';
	}else{
		echo '<a href=messages.php?username='.$obj[0]->{'username'}.'&subject=Friend+request&msg=hej&type=1 target="_self">Send friend request</a>';
		// Create invite field
		echo '<br />invite to league:';
		$request = new makeRequest($backend.'api.php?method=league&owner='.$_SESSION['uid'], 'GET');
		$tmp = $request->makeRequest();
		$rbody = $tmp[0];
		//echo print_r($rbody);
		$obj2 = json_decode($rbody);
		$http_code = $tmp[1];
		// Check so there is no errors
		if($http_code == 200 && $obj2[0]->{'id'} != ""){
			// Create dropdown list with all the leagues the current user owns
			echo '<form method="get" action="messages.php"><select name="leagueid">';
			foreach($obj2 as &$value){
				echo "<option value=".$value->{'id'}.">".$value->{'name'}."</option>";
			}
			echo '</select>
			<input type="hidden" value="'.$obj[0]->{'id'}.'" name="userid" />
			<input type="hidden" value="2" name="type" />
			<input type="submit" Value="Submit" name="submit" /></form>';
		}
	}
}else{
	echo "User not found!";
}

require 'includes/footer.php';
?>